<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/8/18
 * Time: 14:35
 */
namespace Shop\Api;

use Common\Api\Api;
use Shop\Model\MmzWinnerPepoleModel;

class MmzWinnerPepoleApi extends Api{

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "Shop/MmzWinnerPepole/queryNoPaging";
    /**
     * 查询，分页
     */
    const QUERY = "Shop/MmzWinnerPepole/query";
    /**
     * 查询，分页带数量
     */
    const QUERY_WITH_COUNT = 'Shop/MmzWinnerPepole/queryWithCount';

    /**
     * 添加
     */
    const ADD = "Shop/MmzWinnerPepole/add";
    /**
     * 保存
     */
    const SAVE = "Shop/MmzWinnerPepole/save";
    /**
     * 获取信息
     */
    const GET_INFO = "Shop/MmzWinnerPepole/getInfo";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/MmzWinnerPepole/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/MmzWinnerPepole/delete";

    /**
     * 根据UID查询赠无限评价
     */
    const QUERY_WITH_PRODUCT_BY_USER = "Shop/MmzWinnerPepole/queryWithProductByUser";

    protected function _init(){
        $this->model = new MmzWinnerPepoleModel();
    }

    public function queryWithProductByUser($uid, $page = ['curpage'=>0,'size'=>10],$order = false){
        $query = $this->model;
        $query = $query->alias('c')->join('__PRODUCT__ as p ON c.goods_id = p.id','LEFT')
            ->join('common_member as m ON p.uid = m.uid','LEFT');
        if($order!==false){
            $query = $query->order($order);
        }
        $query->field('c.*,p.*,m.nickname');
        $list = $query->where(['c.uid'=>$uid])
            ->page($page['curpage'] . ',' . $page['size'])
            ->select();

        $count = $this->model->alias('c')
            ->join('__PRODUCT__ as p ON c.goods_id = p.id','LEFT')
            ->where(['c.uid'=>$uid])
            ->count();
        if($list === false){
            $error = $this->model->getDbError();
            return $this->apiReturnErr($error);
        }else{
            return $this->apiReturnSuc(['count'=>$count,'list'=>$list]);
        }
    }

}